package me.zhengjie.config;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;
import java.io.File;

/**
 * Description: 上传文件时，配置临时文件所在地址 .<br>
 * {@code IOException} Could not parse multipart servlet request; nested exception is java.io.IOException: The
 * temporary upload
 * location [/tmp/tomcat.7333297176951596407.9000/work/Tomcat/localhost/ROOT] is not valid.
 * Becase Dir '/tmp' cleared By centos.
 *
 * @author jm
 * @see
 * <a href="https://blog.csdn.net/llibin1024530411/article/details/79474953">SpringBoot项目的The temporary upload location ***is not valid 问题</a>
 * @since 2020/1/20
 */
@Configuration
public class MultipartConfig {

    /**
     * 文件上传临时路径
     */
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.home") + "/.jiaoma/file/tmp";
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
}
